<?php

class Payment extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'yii_payments':
	 * @var integer $id
	 * @var integer $user_id
	 * @var string $payment_date
	 * @var string $payment_sum
	 * @var integer $faktura_number
	 * @var integer $admin_id
	 * @var string $admin_ip
	 * @var string $comment
	 * @var integer $active
	 * @var string $change_time
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{payments}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, payment_date, payment_sum, faktura_number, admin_id, admin_ip, change_time', 'required'),
			array('user_id, faktura_number, admin_id, active', 'numerical', 'integerOnly'=>true),
			array('payment_sum', 'length', 'max'=>12),
			array('admin_ip', 'length', 'max'=>15),
			array('comment', 'length', 'max'=>128),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'admin'=>array(self::BELONGS_TO,'Admin','admin_id',
						'select'=>'id,username',),
			'user'=>array(self::BELONGS_TO,'User','user_id',
						'select'=>'id,contract,username',),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => Yii::t('payment', 'Id'),
			'user_id' => Yii::t('payment', 'Contract'),
			'payment_date' => Yii::t('payment', 'Payment Date'),
			'payment_sum' => Yii::t('payment', 'Payment Sum'),
			'faktura_number' => Yii::t('payment', 'Faktura number'),
			'admin_id' => Yii::t('payment', 'Admin'),
			'admin_ip' => Yii::t('payment', 'Admin Ip Address'),
			'comment' => Yii::t('payment', 'Comment'),
			'active' => Yii::t('payment', 'Active'),
			'change_time' => Yii::t('payment', 'Change Time'),
		);
	}
	
	//Корректировка отображения даты
	public function behaviors()
	{
		return array('datetimeI18NBehavior' => array('class' => 'ext.DateTimeI18NBehavior')); // 'ext' is in Yii 1.0.8 version. For early versions, use 'application.extensions' instead.
	}
	
	public function initialDataFilter()
	{
		$filters = new CDataFilter($this,'f');
		$filters->addFilter(new CFilterSearch('s'), Yii::t('payment', 'Search'));
		
        $filters->addFilter(new CFilterDropdown('m', array(
        	'emptyValue'=>Yii::t('payment', 'All'),
        	'displayName'=>Yii::t('payment', 'Admin'),
        )), Yii::t('payment', 'Filter'));
        
        $filters->addFilter(new CFilterLink('a', Yii::t('payment', 'All Payments'), 'all', true), Yii::t('payment', 'Show'));
        $filters->addFilter(new CFilterLink('a', Yii::t('payment', 'Deleted Payments'), 'not_active'), Yii::t('payment', 'Show'));
		
        return $filters;
	}
	// Returns list of searchable fileds for DataFilter widget
	public function getDataFilterSearchFields($filterName)
    {
        switch ($filterName) {
            case 's': //filter name
                return array(
                    'user.contract'=>Yii::t('payment', 'Contract'), //field name => display name
                    'user.username'=>Yii::t('payment', 'User Name'),
                    't.faktura_number'=>Yii::t('payment', 'Faktura number'),
                	't.comment'=>Yii::t('payment', 'Comment'),
                	't.payment_date'=>Yii::t('payment', 'Payment Date'),
                );
        }
    }

    // Returns options for DataFilter widget
    public function getDataFilterOptions($filterName)
    {
        switch ($filterName) {
           case 'm':  //filter name
                // data from database
                return CHtml::listData(Admin::model()->active()->findAll(), 'id', 'username');
        }      
    }
	
    // Applies search criteria enterd using DataFilter widget
    public function applyDataSearchCriteria(&$criteria, $filterName, $searchField, $searchValue)
    {
        if($filterName == 's') {
            $localCriteria = new CDbCriteria;
            //$localCriteria->condition = ' '.$searchField.' LIKE "%'.$searchValue.'%" ';
            $localCriteria->condition = ' '.$searchField.' LIKE :searchValue ';
            $localCriteria->params = array(':searchValue'=>'%'.$searchValue.'%');
            $criteria->mergeWith($localCriteria);
        }
    }
    
    // Applies filter criteria enterd using DataFilter widget
    public function applyDataFilterCriteria(&$criteria, $filterName, $filterValue)
    {	
        if($filterName == 'm') {
            $localCriteria = new CDbCriteria;
            if ($filterValue != 'null') {
            	CDataFilter::setCondition('admin_id', $filterValue, $localCriteria);
            } else {
            	//$localCriteria->with(array('packet'=>array('condition'=>'packet.active=0')));
            	$localCriteria->condition = 'admin.active=0';
            }
            $criteria->mergeWith($localCriteria);
        }

        if($filterName == 'a') {
            if ($filterValue == 'not_active') {
            	$criteria->addCondition('t.active=0','OR');
            }
        }
    }
	
}